package com.horay.nettydemo.nio.buffer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.unit.DataSize;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.IntBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * MappedByteBuffer案例
 *
 * @author heyingcheng
 * @date 2019/11/23 16:52
 */
@Slf4j
public class BasicMappedByteBuffer {

    public static void main(String[] args) {
        try(RandomAccessFile randomAccessFile = new RandomAccessFile("D:/hello.txt", "rw")) {
            FileChannel channel = randomAccessFile.getChannel();
            MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_WRITE, 0, 5);
            map.put(1, (byte) 'h');
            map.put(2, (byte) 'l');
        } catch (FileNotFoundException e) {
            log.error("获取文件异常", e);
        } catch (IOException e) {
            log.error("获取文件异常", e);
        }
    }

}
